// Artur Bednarczyk i Bartłomiej Jędrasiak


package application.DatabaseBackupModule;

import java.io.IOException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class BackupManager {
	
	Runtime runtime;	
	Process proces;
	File file;

	String backupName;	
	String absolutePath;	
		
	ArrayList<String> backupList = new ArrayList<String>();
	
	public BackupManager(){		
		this.readLog();
	}
	
	public void createBackup() throws IOException{
		this.runtime = Runtime.getRuntime();
		this.proces = runtime.exec("cmd /c start "+getAbsolutePath("createBackup.bat")+" "+getAbsolutePath(backupName)+"");
	}
	
	public void useBackup(String backupName) throws IOException{
		this.runtime = Runtime.getRuntime();
		this.proces = runtime.exec("cmd /c start "+getAbsolutePath("useBackup.bat")+" "+getAbsolutePath(backupName)+"");
	}
	
	public String getAbsolutePath(String name){
		file = new File("src/backups" + File.separator + name);	
		return file.getAbsolutePath();
	}
	
	public void readLog(){
		FileReader fr = null;
		try {
			fr = new FileReader(getAbsolutePath("log.txt"));
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		}
		BufferedReader br = new BufferedReader(fr);
		
		String currentLine;
		String tmpLine = null;

		try {
			while((currentLine = br.readLine()) != null)
			{
				currentLine = currentLine.substring(0, 10);
				if(!currentLine.equals(tmpLine)){
					backupList.add(currentLine);
				}
				tmpLine=currentLine;
			}fr.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}	
	
	public ArrayList<String> getBackupList(){
		return backupList;
	}


	
	
	
	

}



